var oldValue = -10; // TODO: set to 0
var check;
var count = 0;
reqObj = null;

window.onload = checkForMessages();

function checkForMessages() {

	checkAvailable();

}

function checkAvailable() {

	if (window.XMLHttpRequest) {
		reqObj = new XMLHttpRequest();
	} else {
		reqObj = new ActiveXObject("Microsoft.XMLHTTP");
	}

	reqObj.onreadystatechange = process;
	reqObj.open("POST", "ajaxUnreadCount", true);
	reqObj.send(null);
}

function process() {
	if (reqObj.readyState == 4) {
		var newValue = reqObj.responseText;
		// $.dNotify('good', "A frocio!!!! ti è arrivato un messaggio!!",
		// {duration: 3000});
		if (newValue != oldValue) {
			// alert("a frociooooo!!!!!!! ti è arrivato un messaggio!!");

			// $.dNotify('good', "A frocio!!!! ti è arrivato un messaggio!!",
			// {duration: 3000});

		}
		oldValue = newValue;
		count = reqObj.responseText;
		if (count > 0) {
			changeClass("message",count);
			document.getElementById("msg_count").innerHTML = reqObj.responseText;
		} else {
			changeClass("message",count);
			document.getElementById("msg_count").innerHTML = '';
		}
	}
}

function changeClass(id,count) {
	if (count > 0)
		parent.document.getElementById(id).className = 'notify';
	else
		parent.document.getElementById(id).className = '';
}

check = setInterval(checkForMessages, 4000);